<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Live;
use App\library\services\AgentService;
use App\library\services\LiveService;
use App\Models\Agent;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class LiveController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Live(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('live_no');
            $grid->column('uid')->display(function ($uid) {
                return Agent::where('id',$uid)->value('nick_name');
            });
            $grid->column('live_img')->image();
            $grid->column('status');
            $grid->column('is_record');
            $grid->column('sort');
            $grid->column('start_time');
            $grid->column('end_time');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Live(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('live_no');
            $show->field('uid');
            $show->field('push_url');
            $show->field('pull_url');
            $show->field('desc');
            $show->field('live_img');
            $show->field('follow_num');
            $show->field('like_num');
            $show->field('online_num');
            $show->field('status');
            $show->field('is_record');
            $show->field('sort');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        //经纪下拉
        $agents = AgentService::getAgents();
        $agent = [];
        foreach ($agents as $value) {
            $agent[$value['id']] = $value['nick_name'];
        }
        //自动生成房间号
        $live_no = 0;
        while (1) {
            $live_no = mt_rand(100000,999999);
            $bool = LiveService::hasLiveNo($live_no);
            if (empty($bool)) {
                break;
            }
        }
        return Form::make(new Live(), function (Form $form) use($agent,$live_no) {
            $form->display('id');
            $form->text('title');
            $form->text('live_no')->default($live_no);
            $form->select('uid')->options($agent);
            $form->text('desc');
            $form->image('live_img')->saveFullUrl();
            $form->text('follow_num');
            $form->text('like_num');
            $form->text('online_num');
            $form->radio('status')->options([1 => '正在直播', 2 => '直播结束'])->default(2);
            $form->radio('is_record')->options([0 => '否', 1 => '是'])->default(1);
            $form->text('sort');
            $form->datetime('start_time');
            $form->datetime('end_time');
            $form->display('created_at');
            $form->display('updated_at');

            $form->saved(function (Form $form, $result) {
                // 自增ID
                if ($form->isCreating()) {
                    $liveInfo = LiveService::getInfo($result);
                }elseif ($form->isEditing()) {
                    $liveInfo = $form->model()->toArray();
                }
                $live['push_url'] = LiveService::getUrl($liveInfo['live_no'],strtotime($liveInfo['end_time']),1);
                $live['pull_url'] = LiveService::getUrl($liveInfo['live_no'],strtotime($liveInfo['end_time']),2);
                LiveService::saveUrl($live,$liveInfo['id']);
            });
        });
    }
}
